package wschat.tcp;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

/**
 * Created by alex on 15-1-26.
 */
@Component
public class ClientHandler extends SimpleChannelInboundHandler<String> {

    public static final Logger LOG = LoggerFactory.getLogger(ClientHandler.class);
    
    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        LOG.info("client handler connect.....");
        //super.channelActive(ctx);
        ctx.channel().writeAndFlush("are you ok");
    }

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, String message) throws Exception {
        LOG.info("msg : {}" + message);
        
        Thread.sleep(5000);

        ctx.channel().writeAndFlush("okokokokok");
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        cause.printStackTrace();
        ctx.close();
    }
}
